/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mdb;

import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import messageClasses.PriceMessage;
import messageClasses.ShipmentMessage;
import messageClasses.StatusMessage;
import plains.CustomerPlain;
import services.BusinessInterface;
import view.ViewFacade;
/**
 *
 * @author ceu
 */
@MessageDriven(mappedName = "jms/Queue", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MessageBean implements MessageListener {
    
    @EJB
    private ViewFacade viewFacade;
    
    static final Logger logger = Logger.getLogger("MessageBean");
    @Resource
    public MessageDrivenContext mdc;
    int SHIPMENT = 1;
    static int PRICE = 2;
    static int STATUS = 3;
    
    
    
    public MessageBean() {
    }
    
    @Override
    public void onMessage(Message inMessage) {
        
        
     TextMessage msg = null;

        try {
            if (inMessage instanceof TextMessage) {
                msg = (TextMessage) inMessage;
                logger.info("MESSAGE BEAN: Message received: " + msg.getText());
                String message = msg.getText();
                int operation = getOperationFromMessage(message);
                //TODO: comprobar que tiene el numero de campos necesarios
                switch (operation){
                    case 1:
                        System.out.println("message bean recibe Shipment message " + msg.getText());
                        viewFacade.lanzarProcesoShipment(message);
                        break;
                        
                    case 2:
                        System.out.println("message bean recibe Price message " + msg.getText());
                        viewFacade.lanzarProcesoPrecio(message);                       
                        break;
                    
                    case 3: 
                       System.out.println("message bean recibe Status message " + msg.getText());
                       viewFacade.lanzarProcesoStatus(message);
                       
                }
                
                
            } else {
                logger.warning(
                        "Message of wrong type: "
                        + inMessage.getClass().getName());
            }
        } catch (JMSException e) {
            logger.severe(
                    "MessageBean.onMessage: JMSException: " + e.toString());
            e.printStackTrace();
            mdc.setRollbackOnly();
        } catch (Throwable te) {
            logger.severe("MessageBean.onMessage: Exception: " + te.toString());
            te.printStackTrace();
        }
    }

    private int getOperationFromMessage(String message) {
        String[] split = message.split(";");
        String op = split[0];
        return Integer.parseInt(op);
    }

   
}
